package br.com.rglariguet.pioneer.controller.asc;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import br.com.rglariguet.pioneer.controller.ControllerComum;
import br.com.rglariguet.pioneer.dao.PecaDAO;
import br.com.rglariguet.pioneer.modelos.Peca;
import br.com.rglariguet.pioneer.util.PecaUtil;

import com.google.common.base.Strings;

@Controller
@RequestMapping("/seguro/asc/")
public class PecaController extends ControllerComum {

	@Autowired
	private PecaDAO pecaDao;


	@RequestMapping("consultaPecas")
	public ModelAndView abreTelaConsultaPecas() {
		ModelAndView mv = new ModelAndView("/seguro/asc/consultaPecas");
		mv.addObject("peca", new Peca());
		return mv;
	}

	@RequestMapping("consultarPeca")
	public String consultaPeca(Peca peca, Model model) {
		List<Peca> pecas = new ArrayList<Peca>();
		if (!Strings.isNullOrEmpty(peca.getCodigo())) {
			if(PecaUtil.validaPecaComTraco(peca.getCodigo()) && PecaUtil.pecaExiste(peca.getCodigo(), pecaDao)){
				pecas.add(pecaDao.buscaPorCodigo(PecaUtil.recuperaCodigoDaPecaComDescricaoConcatenada(peca.getCodigo())));
			}
		}
		model.addAttribute("pecasPesquisadas", pecas);
		return "/seguro/asc/consultaPecas";
	}
}
